package com.thunder.soft.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.thunder.soft.R;
import com.thunder.soft.bean.SystemDataBean;
import com.thunder.soft.common.base.BaseActivity;
import com.thunder.soft.common.util.ViewModelProviderHelper;
import com.thunder.soft.databinding.ActivitySystemItemBinding;
import com.thunder.soft.ui.adapter.SystemItemAdapter;
import com.thunder.soft.vm.SystemItemViewModel;

public class SystemItemActivity extends BaseActivity<ActivitySystemItemBinding> {

    private SystemItemViewModel viewModel;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent=getIntent();
        int cid=intent.getIntExtra("cid",60);
        viewModel.cid=cid;
    }

    @Override
    protected void initData() {
        viewModel = ViewModelProviderHelper.buildViewModel(this, SystemItemViewModel.class);
        getLifecycle().addObserver(viewModel);
        binding.setViewModel(viewModel);

        viewModel.getErrorMsg().observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                Toast.makeText(SystemItemActivity.this, s, Toast.LENGTH_SHORT).show();
            }
        });

        SystemItemAdapter systemItemAdapter = new SystemItemAdapter();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        binding.systemItemRv.setLayoutManager(linearLayoutManager);
        binding.systemItemRv.setAdapter(systemItemAdapter);

        viewModel.getLiveData().observe(this, new Observer<SystemDataBean>() {

            @Override
            public void onChanged(SystemDataBean systemDataBean) {
                systemItemAdapter.addList(systemDataBean.getDatas());
            }
        });

        // 关闭页面
        viewModel.getFinishActivity().observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                finish();
            }
        });
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_system_item;
    }
}